package entity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import logic.PasswordGenerator;

public class OperatorDAO implements IOperatorDAO
{
	private List<OperatorDTO> operators = new ArrayList<OperatorDTO>();
	private static int nextId = 11;
	
	public OperatorDAO() {
		operators.add(new OperatorDTO(10, "Admin", "000000-0000", "02324it!"));
		operators.add(new OperatorDTO(11, "Marvin The sad Andriod", "010101-0101", PasswordGenerator.generatePassword()));
		operators.add(new OperatorDTO(12, "Douglas Adams", "123456-7890", PasswordGenerator.generatePassword()));
		operators.add(new OperatorDTO(13, "Slartibartfast", "999999-9999", PasswordGenerator.generatePassword()));
		operators.add(new OperatorDTO(14, "Zaphod Beeblebrox", "098765-4321", PasswordGenerator.generatePassword()));
		operators.add(new OperatorDTO(15, "Ford Prefect", "001122-3344", PasswordGenerator.generatePassword()));
		operators.add(new OperatorDTO(16, "Arthur Dent", "556677-8899", "test"));
		nextId = 17;
	}

	@Override
	public OperatorDTO getOperator(int oprId) throws DALException {
		for (OperatorDTO operatorDTO : operators)
			if (operatorDTO.getOprID() == oprId)
				return operatorDTO;

		return null;
	}

	@Override
	public List<OperatorDTO> getOperatorList() throws DALException {
		return operators;
	}

	@Override
	public OperatorDTO createOperator(OperatorDTO opr) throws DALException {
		if(existsCpr(opr.getCpr()))
			throw new DALException(1); // cpr already exists
		
		// making sure that users are always given a new unique ID.
		if(nextId > 11 || nextId < 99){
			opr.setOprID(nextId);
			operators.add(opr);
			nextId += 1;
			return opr;
		} else{
			throw new DALException(4);
		}
	}
	
	private boolean existsCpr(String cpr) {
		for(Iterator<OperatorDTO> iterator = operators.iterator(); iterator.hasNext();)		
			if(iterator.next().getCpr().equals(cpr))
				return true;
		
		return false;
	}

	@Override
	public void updateOperator(OperatorDTO opr) throws DALException {
		for(Iterator<OperatorDTO> iterator = operators.iterator(); iterator.hasNext();) {
			OperatorDTO oldOpr = iterator.next();
		
			if(opr.getOprID() == oldOpr.getOprID()){
				oldOpr.setOprNavn(opr.getOprNavn());
				oldOpr.setCpr(opr.getCpr());
				oldOpr.setPassword(opr.getPassword());
				return;
			}
		}
		throw new DALException(2);
	}

	@Override
	public void deleteOperator(int oprId) throws DALException {
		for(Iterator<OperatorDTO> iterator = operators.iterator(); iterator.hasNext();)
			if(iterator.next().getOprID() == oprId){
				iterator.remove();
				return;
			}
			
		throw new DALException(2);
	}
	
	@Override
	public void changePassword(int oprId, String[] passwords) throws DALException{
		for(OperatorDTO opr : operators)
			if(opr.getOprID() == oprId)
				if(opr.getPassword().equals(passwords[0])){
					opr.setPassword(passwords[1]);
					return;
				}
				else
					throw new DALException(3);
		
		throw new DALException(2);
	}
}
